package main

import (
	"fmt"
	"imooc/studygo/retriever/mock"
	"imooc/studygo/retriever/real"
	"time"
)

type Retriever interface{   //接口的使用由   使用者来决定
	Get (url string) string
}

type Poster interface {
	Post(url string, from map[string]string) string
}

const url = "http://www.imooc.com"

func Download(r Retriever) string {
	return r.Get(url)
}

func post(poster Poster)  {
	poster.Post(url, map[string]string {
		"name": "ccmouse",
		"course": "golang",
	})
}

type RetrieverPoster interface {
	Retriever
	Poster
	//other
}

func session(s RetrieverPoster) string {
	//s.Post()
	//s.Get()

	s.Post(url, map[string]string{
		"contents": "hahahahahahhaah",
	})
	return s.Get(url)
}

func inspect(r Retriever) {
	fmt.Println("Inspectiing", r)
	fmt.Printf(" > %T %v \n", r, r)
	fmt.Print(" > Type switch:")
	switch v := r.(type) {
	case *mock.Retriever:
		fmt.Println("Contents:", v.Content)
	case *real.Retriever:
		fmt.Println("UserAgent:", v.UserAgent)
	}
	fmt.Println()
}

func main() {
	var r Retriever
	re := mock.Retriever{"this is a fake imooc.com"}
	r = &re
	inspect(r)
	r = &real.Retriever{
		UserAgent: "Mozilla/5.5",
		TimeOut: time.Second,
	}
	inspect(r)
fmt.Println("--------------line---------------")
	//type assertion
	if mockRetriever, ok := r.(*mock.Retriever); ok {
		fmt.Println(mockRetriever.Content)
	}else {
		fmt.Println("not a mock retriever")
	}

fmt.Println("Try a session")
	fmt.Println(session(&re))

	//fmt.Println(Download(mock.Retriever{"this is another a fake imooc.com"}))

	fmt.Println()

}
